package com.jacobson.core.framework.configuration

import android.app.Application
import retrofit2.Retrofit

/**
 * ================================================
 * Retrofit配置类
 * @author Wucz on 2018/7/18 17:59
 * ================================================
 */

class RetrofitConfiguration internal constructor(builder: Builder) {
    internal var baseUrl: String? = null
    internal var onBuildInterceptor: OnBuildInterceptor? = null

    init {
        builder.let {
            baseUrl = builder.baseUrl
        }
    }

    class Builder internal constructor(init: Builder.() -> Unit) {
        internal var baseUrl: String? = null
        private var onBuildInterceptor: OnBuildInterceptor? = null

        init {
            init()
        }

        fun baseUrl(closure: () -> String): Builder {
            this.baseUrl = closure()
            return this
        }

        fun onBuildInterceptor(closure: () -> OnBuildInterceptor): Builder {
            this.onBuildInterceptor = closure()
            return this
        }

        fun build(): RetrofitConfiguration {
            return RetrofitConfiguration(this)
        }
    }

    interface OnBuildInterceptor {
        fun onBuildIntercept(application: Application, builder: Retrofit.Builder)
    }

    companion object {
        fun build(init: Builder.() -> Unit): RetrofitConfiguration = Builder(init).build()
    }
}